package cn.uppower.wordaliy.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.adapter.CalendarAdapter;
import cn.uppower.wordaliy.model.CalendarDate;
import cn.uppower.wordaliy.model.enu.CalendarStatus;
import cn.uppower.wordaliy.util.CalendarUtil;

/**
 * Created by elitetyc on 2018/11/26.
 * Describe:
 */
public class WordaliyCalendar extends LinearLayout {

    private TextView tv_year_month;

    private int lxqd= 0;

    public void setLxqd(int lxqd) {
        this.lxqd = lxqd;
    }

    public void setLjqd(int ljqd) {
        this.ljqd = ljqd;
    }

    public int getLxqd() {
        return lxqd;
    }

    public int getLjqd() {
        return ljqd;
    }

    private int ljqd = 0;

    private View yearAndMonth,week,tips;
    private RecyclerView rvDateList;
    private Context mContext;
    private CalendarAdapter calendarAdapter;
    private List<CalendarDate> calendarDateList;


    public WordaliyCalendar(Context context) {
        super(context);
    }

    public WordaliyCalendar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        initView();
    }



    public WordaliyCalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void initView() {

        this.setOrientation(VERTICAL);
        calendarDateList = new ArrayList<>();
        calendarAdapter = new CalendarAdapter(calendarDateList,mContext);
        yearAndMonth=View.inflate(mContext, R.layout.custom_year_month,null);
        tv_year_month = yearAndMonth.findViewById(R.id.tv_year_month);
        getDateList(new Date());
        week=View.inflate(mContext, R.layout.custom_week,null);
        rvDateList = new RecyclerView(mContext);
        rvDateList.setLayoutManager(new GridLayoutManager(mContext,7));
        rvDateList.setAdapter(calendarAdapter);
        tips=View.inflate(mContext, R.layout.custom_tips,null);



        this.addView(yearAndMonth);
        this.addView(week);
        this.addView(rvDateList);
        this.addView(tips);



    }

    private void getDateList(Date date) {
        Random random = new Random();
        tv_year_month.setText(CalendarUtil.getYearAndMonth(date));
        calendarDateList.clear();
        int isWeek = CalendarUtil.getDayIsWeek(date);
        int days = CalendarUtil.getDaysOfMonth(date);
        int day = CalendarUtil.getDayForDate(date);

        for (int i=1;i<isWeek;i++){
            calendarDateList.add(new CalendarDate(0,CalendarStatus.KONGBAI));
        }
        for (int i=1;i<=days;i++){

            if (i==day){
                calendarDateList.add(new CalendarDate(i,CalendarStatus.TODAY));
            }else if(i>day){
                calendarDateList.add(new CalendarDate(i,CalendarStatus.OTHER));
            }else {
                if (random.nextBoolean()){
                    calendarDateList.add(new CalendarDate(i,CalendarStatus.YI_QIANDAO));
                }else {
                    calendarDateList.add(new CalendarDate(i,CalendarStatus.WEI_QIANDAO));
                }

            }


        }
        getDateNumbers(day,isWeek);
        calendarAdapter.notifyDataSetChanged();
    }

    public void getDateNumbers(int day,int week) {
        //        获取累计签到天数
        for (CalendarDate c:calendarDateList){
            if (c.getCalendarStatus()==CalendarStatus.YI_QIANDAO){
                ljqd++;
            }
        }
//        获取连续签到天数
        for (int i=day+week-3;i>0;i--){
            boolean isOk=false;
            switch (calendarDateList.get(i).getCalendarStatus()){
                case YI_QIANDAO:
                    lxqd++;
                    break;
                default:
                    isOk=true;
                    break;
            }

            if (isOk){
                break;
            }

        }
    }


}
